package com.nineclock.common.exception;


import com.nineclock.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//统一处理异常类 @ControllerAdvice:当我的controller出现异常的时候通知这个类来执行
//@ResponseBody把我响应的数据转换成JSON(如果是对象转换成JSON)
@RestControllerAdvice
@Slf4j

public class BasicExceptionAdvice {
    //捕获NcException,预料到的,进行处理
    @ExceptionHandler({
            NcException.class
    })
    public Result handlerNcException(NcException e){
        log.error("错误状态码:" +e.getCode());
        log.error("异常信息" + e.getMessage());
        return Result.errorCodeMessage(e.getCode(),e.getMessage());
    }

    //捕获其他异常(Exceotion)
    public Result handlerOtherException(Exception e){
       e.printStackTrace();
        return Result.errorCodeMessage(500,"系统异常,请联系管理员");
    }

}
